package com.jar.exercise.多线程.线程停止;

/**
 * 线程停止的几种操作
 */
public class Demo {
	public static void main(String[] args) {
		try {
			Demo2 demo2 = new Demo2();
			demo2.start();
			Thread.sleep(3000);
			demo2.interrupt();
		} catch (InterruptedException e) {
		}

	}
}

class Demo2 extends Thread {
	private Boolean flag = true;

	@Override
	public void run() {
		int i = 0;
		while (flag) {
			try {
				System.out.println(isInterrupted() + "--" + Thread.currentThread().getName() + "：" + i++);
				Thread.sleep(1000);
				if (isInterrupted()) {
					break;
				}
			} catch (InterruptedException e) {
				System.out.println("阻塞中断" + Thread.currentThread().isInterrupted());//显示false并抛异常
				return;
			}

		}
	}

	public void setFlag() {
		this.flag = false;
	}
}
